<?php
class Producttype extends MY_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
	}
	
	/**
	 * 管理首页
	 * Enter description here ...
	 */
	public function index($page = 0){
		
		$data['title'] = '产品分类';
		$per_page = 20;
		
		$admins = $this->db->get('producttype');
		
//		print_r($admins);
		$data['list_current'] = array('product','producttype');
		$data['js'] = array('producttype');
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = '/sysadmin/producttype/index';
		$config ['total_rows'] = $admins->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$a = $this->db->query("SELECT a.*, case when a.parentid=0 then '-顶级-' else b.title end as pname FROM {$this->db->dbprefix}producttype a 
		left join {$this->db->dbprefix}producttype b 
		on a.parentid=b.id LIMIT $page, $per_page");
		$articletype = $a->result();
		$data['articletype'] = $articletype;
		
		$data['pageinfo'] =  $this->pagination->create_links ();		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/producttype', $data);
		$this->load->view('admin/footer');
	}
	function search(){
		$a = $this->db->get('producttype');
		echo json_encode($a->result());
	}
	function add($id=0){
		if($this->input->post('title')){
			$setarr = array('title'=>$this->input->post('title'),
				'parentid'=>intval($this->input->post('parentid')));
			if(!$this->input->post('id')){//添加
				$this->db->insert('producttype', $setarr);
				echo $this->db->insert_id(); 
				die();
			}else{
				$this->db->where('id', $this->input->post('id'));
				if($this->db->update('producttype', $setarr)){
					echo 1;
				}else{
					echo -1;
				}
			}
		}else{
			if($id){//编辑时
				$a = $this->db->query("select a.*, case when a.parentid=0 then '-顶级-' else b.title end as pidSel
				from {$this->db->dbprefix}producttype a left join {$this->db->dbprefix}producttype b
				on a.parentid=b.id
				where a.id=$id");
				echo json_encode($a->result());
			}else{
				echo '-1';
			}
			
		}
	}
	
	
	public function delete($id = NULL){
		if($id){
			$id = explode('-', $id);
			$this->db->where_in('id', $id);
			if($this->db->delete('producttype')){
				echo 1;
			}else{
				echo -1;
			}
			
		}
	}
	
	
}
// END  Class

/* End of file producttype.php */
/* Location: ./application/controller/sysadmin/producttype.php */
